<?php require_once dirname(__FILE__) . "/shared/config/config.php"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
        <title>Briefing Papers - Tesdo</title>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php'; ?>
        <link href="<?php echo LINK_ROOT.DIR_LAYOUT_CSS; ?>/briefing_papers.css" rel="stylesheet" />
        <meta name="viewport" content="width=device-width" />
        <?php
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_briefing_paper.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_news.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . "lib_pager.php";

        $news_access = new dao_news();
        $briefing_access = new dao_briefing_paper();
        $lib_pager = new lib_pager();

        $papers_archive = $briefing_access->get_all();
        $page_title = "Briefing Papers List";
        $briefs = $briefing_access->get_all();

        if (!empty($_GET['arc_conferences_id'])) {
            
        } else if (!empty($_GET['brief_id'])) {
            $brief_id = $_GET['brief_id'];
            $brief = $briefing_access->get_by_id($brief_id);
            $page_title = "Briefing Paper Detail";
        }
        ?>
    </head>
    <body>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php'; ?>

        <!-- Body -->
        <div id="body">
            <section class="content-wrapper main-content clear-fix">
                <div class="DetailPage">
                    <hgroup class="title">
                        <h1>
                            <span id="MainContent_lblTitle">
                                <?php echo $page_title; ?>
                            </span></h1>
                        <h2></h2>
                    </hgroup>
                    <article class="CenterArea">
                        

                        <?php
                        if (!empty($_GET['brief_id'])) {
                            $item = $brief;
                            ?>
                            <div id="MainContent_AbstractDeail" class="AbstractDeail">
                                <h4><span id="MainContent_lblAbsTitle"><?php echo $item["brief_name"]; ?></span></h4>
                                <hr>
                                <h5>Author:</h5>
                                <p> <span id="MainContent_lblAuthor"><?php echo $item["author"]; ?> </span></p>
                                <h5>Description:</h5>
                                <p> <span id="MainContent_lblAuthor"><?php echo $item["detail"]; ?> </span></p>
                                <h5>Added on:</h5>
                                <p>  <span id="MainContent_lblCitation"><?php echo $item["Addedon"]; ?></span></p>
                                <hr>
                                <a id="MainContent_PDFFile" title="Download File" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_NEWS_FILES . $item["file_upload"]; ?>" target="_blank">Download File</a>
                            </div>
                            <?php
                        }
                        ?>

                        <?php
                        if (!empty($_GET['year'])) {
                            $year = $_GET['year'];
                            $current_page = 1;
                            $page_size = 10;

                            if (!empty($_GET['page'])) {
                                $current_page = $_GET['page'];
                            }

                            $list_total = $briefing_access->get_paper_by_year($year);
                            $total_page = $lib_pager->get_total_page($list_total, $page_size);
                            $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                            
                                
                            
                            ?>
                                <div class="year_move_box">
                                    <div class="previous">
                                        <h3><a href="briefing_papers.php?year=<?php echo $year - 1; ?>">previous year</a></h3>
                                    </div>
                                    <div class="current_year">
                                        <h3><?php echo $year; ?></h3>
                                    </div>
                                    <?php
                                    $year_now = date("Y");
                                    if ($year < $year_now) {
                                    ?>
                                    <div class="next">
                                        <h3><a href="briefing_papers.php?year=<?php echo $year + 1; ?>">next year</a></h3>
                                    </div>
                                    <?php
                                    }
                                    ?>
                                </div>
                        <div class="clear"></div>
                        <div class="content_list">
                            <?php
                                
                            
                            $count = 0;
                            if (empty($current_page_list) || sizeof($current_page_list) == 0) {
                                echo "Not paper found.";
                            } else {
                                foreach ($current_page_list as $i => $item) {
                                    ?>
                                    <table id="MainContent_dlPapers" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                                        <tbody><tr>
                                                <td>
                                                    <div class="adsSortDetail">
                                                        <div>
                                                            <b> <a href="briefing_papers.php?brief_id=<?php echo $item["brief_id"]; ?>" style="color:blue; font-size:14px;"><?php echo $item["brief_name"]; ?></a></b><br>
                                                            <b>Added on:</b> <?php echo $item["Addedon"]; ?> <br>
                                                            <hr>
                                                            <b><a href="briefing_papers.php?brief_id=<?php echo $item["brief_id"]; ?>" style="color:blue; font-size:14px;">DETAIL</a> | <a style="color:blue; font-size:14px;" target="_blank" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_NEWS_FILES . $item["file_upload"]; ?>">FILE</a></b>
                                                            <hr>
                                                        </div>
                                                    </div></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <?php
                                }
                            }
                            ?>
                        </div>
                            <table>
                                <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                <span>
                                                    <a href="briefing_papers.php?year=<?php echo $year; ?>&page=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                </tbody>
                            </table>
                            <?php
                        }
                        ?>
                    </article>
                    <aside>
                    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'navbar.php'; ?>
                    
                    <div class="RightArea jump">
                        <div id="Archive">
                            <h1>Archive</h1>
                            <div>
                                <table id="MainContent_dlArchive" cellspacing="0" style="border-collapse:collapse;">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <ul>
                                                    <?php
                                                    if (!empty($papers_archive) && sizeof($papers_archive) > 0) {
                                                        $year_array = array();
                                                        foreach ($papers_archive as $i => $item) {
                                                            if (!in_array($item['year'], $year_array) && !empty($item['year'])) {
                                                                array_push($year_array, $item['year']);
                                                                ?>
                                                                <li>
                                                                    <a href="briefing_papers.php?year=<?php echo $item['year']; ?>">
                                                                        <?php echo $item['year']; ?>
                                                                    </a>
                                                                </li>
                                                                <?php
                                                            }
                                                        }
                                                    }
                                                    ?>
                                                </ul>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    </aside>
                </div>
            </section>
        </div>
        <!-- End Body -->

        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php'; ?>
    </body>
</html>